package com.adui.mgt.performance.common;

/**
 * 比较工具类
 *
 */
public class CompareUtil {

	/**
	 * 比较两个对象
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static int compare(Comparable item1, Comparable item2) {
		if (item1 == null && item2 == null) {
			return 0;
		}
		if (item1 == null && item2 != null) {
			return -1;
		}
		if (item1 != null && item2 == null) {
			return 1;
		}
		int result = item1.compareTo(item2);
		if (result < 0) {
			return -1;
		} else if (result > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 比较两个数组
	 */
	@SuppressWarnings("rawtypes")
	public static int compare(Comparable[] array1, Comparable[] array2) {
		if (array1 == null || array2 == null || array1.length != array2.length) {
			throw new IllegalArgumentException();
		}
		for (int i = 0; i < array1.length; i++) {
			switch (compare(array1[i], array2[i])) {
			case -1:
				return -1;
			case 1:
				return 1;
			default:
				break;
			}
		}
		return 0;
	}

	/**
	 * 比较两个对象是否相等
	 */
	public static boolean equals(Object item1, Object item2) {
		if (item1 == null && item2 == null) {
			return true;
		}
		if (item1 == null && item2 != null) {
			return false;
		}
		if (item1 != null && item2 == null) {
			return false;
		}
		return item1.equals(item2);
	}

}
